﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chowpoo.Sql;
using CourseSystem.Core.Entities;
using CourseSystem.Core.SqlServerDAL;

namespace CourseSystem.Core.BLL
{
    public class SectionBLL : CommonBLL<SectionInfo, SectionDAL>
    {
        public static IQueryable<SectionInfo> GetList(int cid)
        {
            var re = GetList().Where(o => o.ChapterId == cid);
            return re;
        }

        public static byte GetMax(int cid)
        {
            using (var dal = new SectionDAL(Settings.ConnectionName))
            {
                var list = dal.Select(o => o.ChapterId == cid);
                if (list.Count < 1)
                    return 0;
                return list.Max(o => o.OrderId);
            }
        }

        public static int GetCount()
        {
            return GetList().Count();
        }

        public static SectionInfo Get(int? id)
        {

            using (var dal = new SectionDAL(Settings.ConnectionName))
            {
                if (id.HasValue)
                {
                    var re = dal.SelectSingle(o => o.Id == id);
                    return re;
                }
                return GetList().OrderBy(o => o.ChapterInfo.OrderId).ThenBy(o => o.OrderId).FirstOrDefault();
            }

        }

        public static bool Del(int id)
        {
            using (var dal = new SectionDAL(Settings.ConnectionName))
            {
                return dal.Delete(o => o.Id == id) != null;
            }
        }

    }
}
